//app.js
const Host = require('config').Host
App({
  onLaunch: function (e) {
    var that = this
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    if (e.query.pid) {
      wx.setStorageSync('senceid', e.query.pid)
    } else {
      wx.setStorageSync('senceid', 0)
    }
    //console.log(wx.getStorageSync('senceid'))
    // 登录
    wx.login({
      success: function (res) {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          wx.request({
            url: Host + 'Index/appid',
            data: { code: res.code },
            method: 'GET',
            success: function (res) {
              //console.log(res)
              var sessionkey = res.data.session_key
              wx.setStorageSync('sessionkey', sessionkey)
              var openid = res.data.openid
              wx.setStorageSync('openid', openid)
              wx.request({
                url: Host + 'Index/getuserid',
                data: { openid: openid },
                success: function (res) {
                  // console.log(res)
                  if (res.data != 0) {
                    var userid = res.data
                    wx.setStorageSync('userid', userid)
                  }
                }
              })

              wx.getUserInfo({
                lang: 'zh_CN',
                success: function (res) {
                  //console.log(res)
                  var useradd = res.rawData
                  var openid = wx.getStorageSync('openid')
                  var senceid = wx.getStorageSync('senceid')
                  wx.setStorageSync('userinfo', res.userInfo)
                  wx.request({
                    url: Host + 'Index/adduser', //录入信息
                    data: { userInfo: useradd, openid: openid, pid: senceid },
                    method: 'get',
                    success: function (res) {
                      // console.log(res)
                      var userid = res.data
                      wx.setStorageSync('userid', userid)
                    }
                  })
                },
                fail: function () {
                  that.globalData.getUserInfoFail = true
                  var fail = that.globalData.getUserInfoFail
                  if (fail == true) {
                    wx.redirectTo({
                      url: '/pages/welcome/welcome'
                    })
                  }

                }
              })

            }
          })
        }
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null
  }
})